﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Control;
using System.IO;
using HiStaff.Util;
using HiStaff.Domain.Sys;
using HiStaff.iSystem;

namespace HiStaff.UI
{
    public partial class Login : Form
    {
        private const string DBAFileExtenName = "*.dba";
        private const string DBA_COLNAME = "DBA";
        private const string DBAVALUE_COLNAME = "DBAVALUE";

        public DialogResult LoginResult { get; set; }
        private int NumberLogin = 0;
        private bool IsFormBeginDragged = false;
        private int MouseDownX;
        private int MouseDownY;
        public Login()
        {
            this.Font = SystemFont.FontSegoeUi;
            InitializeComponent();
            this.hiLabel4.Font = SystemFont.GetFont(SystemFont.FontName.SegoeUiSemiBold, System.Drawing.FontStyle.Italic, 9f);
            this.txtUser.Image = Properties.Resources.user;
            this.txtPassword.Image = Properties.Resources.key;
        }

        private const int CS_DROPSHADOW = 0x00020000;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
            //this.BackColor = System.Drawing.Color.Transparent;
            //this.TransparencyKey = System.Drawing.Color.Violet;
            //this.pictureBox1.BackColor = System.Drawing.Color.Violet;
            //Service.InsuranceClient.Close();
            //Service.PayrollClient.Close();
            //Service.ProfileClient.Close();
            //Service.SystemClient.Close();
            //Service.TimeClient.Close();
            //Service.CommonClient.Close();
        }

        private bool UserLogin()
        {
            if (!string.IsNullOrEmpty(txtUser.Text.Trim()) && !string.IsNullOrEmpty(txtPassword.Text))
            {
                SY_USER objUser = new SY_USER
                {
                    USERNAME = txtUser.Text.Trim(),
                    PASSWORD = txtPassword.Text,
                    STATUS = 1
                };
                var objChk = HiStaff.iSystem.Service.SystemClient.GetUser(objUser);
                return objChk != null;
            }
            else return false;
        }

        private void LoginActive()
        {
            try
            {
                if (UserLogin())
                {
                    HiStaff.Domain.StaticObject.USERNAME = txtUser.Text.Trim();
                    LoginResult = System.Windows.Forms.DialogResult.OK;
                    this.Hide();
                }
                else if (NumberLogin < 2)
                {
                    NumberLogin++;
                    MessageBox.Show("Tên tài khoản hoặc mật khẩu đăng nhập sai.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Bạn đã đăng nhập sai 3 lần liên tiếp.\nHệ thống sẽ tự động thoát.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    LoginResult = System.Windows.Forms.DialogResult.Cancel;
                    this.Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                txtPassword.Focus();
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                LoginActive();
            }
        }

        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                IsFormBeginDragged = true;
                MouseDownX = e.X;
                MouseDownY = e.Y;
            }
        }

        private void Login_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                IsFormBeginDragged = false;
            }
        }

        private void Login_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsFormBeginDragged)
            {
                var tmp = new Point();
                tmp.X = this.Location.X + (e.X - MouseDownX);
                tmp.Y = this.Location.Y + (e.Y - MouseDownY);
                this.Location = tmp;
            }
        }

        private void hiLabel4_MouseHover(object sender, EventArgs e)
        {
            ((HiLabel)sender).BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(109)))), ((int)(((byte)(181)))));
        }

        private void hiLabel4_MouseLeave(object sender, EventArgs e)
        {
            ((HiLabel)sender).BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(43)))), ((int)(((byte)(78)))), ((int)(((byte)(154)))));
        }

        private void hiLabel4_Click(object sender, EventArgs e)
        {
            LoginActive();
        }

        private void hiLabel4_MouseDown(object sender, MouseEventArgs e)
        {
            ((HiLabel)sender).BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(89)))), ((int)(((byte)(136)))), ((int)(((byte)(207)))));
        }

        private void hiLabel4_MouseUp(object sender, MouseEventArgs e)
        {
            ((HiLabel)sender).BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(109)))), ((int)(((byte)(181)))));
        }

        private void hiLabel5_Click(object sender, EventArgs e)
        {
            LoginResult = System.Windows.Forms.DialogResult.Cancel;
            this.Hide();
        }

        private void hiLabel5_MouseHover(object sender, EventArgs e)
        {
            ((HiLabel)sender).ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(109)))), ((int)(((byte)(181)))));
        }

        private void hiLabel5_MouseLeave(object sender, EventArgs e)
        {
            ((HiLabel)sender).ForeColor = System.Drawing.SystemColors.ControlText;
        }
    }
}
